package org.molamil.acto.utils {

	/**
	 * Utility methods to work with Strings.
	 * 
	 * <p>trim(), leftTrim(), rightTrim(), leftTrimForChars() and 
	 * rightTrimForChars() extracted from Prana Framework 
	 * (www.pranaframework.org): org.pranaframework.utils.StringUtils</p>
	 * 
	 * @author Jorge Hernandez
	 * @author Christophe Herreman (Prana Framework)
	 * @author Martin Heidegger (as2lib)
	 * @author Simon Wacker (as2lib)
	 * 
	 * TODO: Contact and notify Christophe Herreman
	 */
	public class StringUtils {
		
		
		// BELOW CODE EXTRACTED FROM PRANA FRAMEWORK (www.pranaframework.org)
		// org.pranaframework.utils.StringUtils
		
		/*
		 * Copyright (c) 2007-2008, the original author(s)
		 * 
		 * All rights reserved.
		 * 
		 * Redistribution and use in source and binary forms, with or without
		 * modification, are permitted provided that the following conditions are met:
		 * 
		 *     * Redistributions of source code must retain the above copyright notice,
		 *       this list of conditions and the following disclaimer.
		 *     * Redistributions in binary form must reproduce the above copyright
		 *       notice, this list of conditions and the following disclaimer in the
		 *       documentation and/or other materials provided with the distribution.
		 *     * Neither the name of the Prana Framework nor the names of its contributors
		 *       may be used to endorse or promote products derived from this software
		 *       without specific prior written permission.
		 * 
		 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
		 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
		 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
		 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
		 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
		 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
		 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
		 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
		 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
		 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
		 */
		
		/**
		 * Removes all empty characters at the beginning and at the end of the passed-in
		 * <code>string</code>.
		 *
		 * <p>Characters that are removed: spaces <code>" "</code>, line forwards <code>"\n"</code>
		 * and extended line forwarding <code>"\t\n"</code>.</p>
		 *
		 * @param string the string to trim
		 * @return the trimmed string
		 */
		public static function trim(string:String):String {
			return leftTrim(rightTrim(string));
		}

		/**
		 * Removes all empty characters at the beginning of a string.
		 *
		 * <p>Characters that are removed: spaces <code>" "</code>, line forwards <code>"\n"</code>
		 * and extended line forwarding <code>"\t\n"</code>.</p>
		 *
		 * @param string the string to trim
		 * @return the trimmed string
		 */
		public static function leftTrim(string:String):String {
			return leftTrimForChars(string, "\n\t\n ");
		}

		/**
		 * Removes all empty characters at the end of a string.
		 *
		 * <p>Characters that are removed: spaces <code>" "</code>, line forwards <code>"\n"</code>
		 * and extended line forwarding <code>"\t\n"</code>.</p>
		 *
		 * @param string the string to trim
		 * @return the trimmed string
		 */     
		public static function rightTrim(string:String):String {
			return rightTrimForChars(string, "\n\t\n ");
		}

		/**
		 * Removes all characters at the beginning of the <code>string</code> that match to the
		 * set of <code>chars</code>.
		 *
		 * <p>This method splits all <code>chars</code> and removes occurencies at the beginning.</p>
		 *
		 * <p>Example:
		 * <code>
		 *   trace(StringUtil.rightTrimForChars("ymoynkeym", "ym")); // oynkeym
		 *   trace(StringUtil.rightTrimForChars("monkey", "mo")); // nkey
		 *   trace(StringUtil.rightTrimForChars("monkey", "om")); // nkey
		 * </code></p>
		 *
		 * @param string the string to trim
		 * @param chars the characters to remove from the beginning of the <code>string</code>
		 * @return the trimmed string
		 */
		public static function leftTrimForChars(string:String, chars:String):String {
			if (string == null)
				return "";
			var from:Number = 0;
			var to:Number = string.length;
			while (from < to && chars.indexOf(string.charAt(from)) >= 0) {
				from++;
			}
			return (from > 0 ? string.substr(from, to) : string);
		}

		/**
		 * Removes all characters at the end of the <code>string</code> that match to the set of
		 * <code>chars</code>.
		 *
		 * <p>This method splits all <code>chars</code> and removes occurencies at the end.</p>
		 *
		 * <p>Example:
		 * <code>
		 *   trace(StringUtil.rightTrimForChars("ymoynkeym", "ym")); // ymoynke
		 *   trace(StringUtil.rightTrimForChars("monkey***", "*y")); // monke
		 *   trace(StringUtil.rightTrimForChars("monke*y**", "*y")); // monke
		 * </code></p>
		 *
		 * @param string the string to trim
		 * @param chars the characters to remove from the end of the <code>string</code>
		 * @return the trimmed string
		 */
		public static function rightTrimForChars(string:String, chars:String):String {
			if (string == null)
				return "";
			var from:Number = 0;
			var to:Number = string.length - 1;
			while (from < to && chars.indexOf(string.charAt(to)) >= 0) {
				to--;
			}
			return (to >= 0 ? string.substr(from, to + 1) : string);
		}
		
		public static function removeTags(txt:String):String {
			while (txt.indexOf("<") != -1) {
				if (txt.indexOf(">") != -1) {
					txt = txt.substring(0, txt.indexOf("<")) + txt.substring(txt.indexOf(">") + 1);
				} else {
					return txt;
				} 
			}
			return txt;
		}
		
		public static function removeLineBreaks(txt:String):String {
			if (txt == null || txt == "")
				return "";
			var s:String = "";
			for (var i:int = 0; i < txt.length; i++) {
				var w:String = txt.charAt(i);
				if (w == '\n')
					continue;
				s += w;
			}
			return s;
		}
		
		
	}
	
}
